﻿using Furion.CMS.Core.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Furion.DatabaseAccessor
{
    public static class QueryableExtension
    {
        /// <summary>
        /// 返回一个分页的查询Queryable
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> AsPagedQuery<TEntity>(this IQueryable<TEntity> entities, int pageIndex = 1, int pageSize = 20) where TEntity : new()
        {
            entities = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return entities;
        }

        /// <summary>
        /// 根据输入排序参数进行排序
        /// <para>
        /// orderBy+thenBy
        /// </para>
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entities"></param>
        /// <param name="inputParams"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> AsSortQuery<TEntity>(this IQueryable<TEntity> entities, BaseSortParams inputParams)
        {
            var sortParam = inputParams?.GetSort();
            if (sortParam != null)
            {
                // 主排序方式
                var orderQuery = sortParam.Mode == SortMode.Ascend ?
                    entities.OrderBy(sortParam.Field) :
                    entities.OrderBy($"{sortParam.Field} DESC");

                // 针对追加排序的处理
                if (!string.IsNullOrWhiteSpace(sortParam.ThenSortField))
                {
                    entities = sortParam.ThenSortMode == SortMode.Ascend ?
                        orderQuery.ThenBy(sortParam.ThenSortField) :
                        orderQuery.ThenBy($"{sortParam.ThenSortField} DESC");
                }
                else
                {
                    entities = orderQuery;
                }
            }
            return entities;
        }

        /// <summary>
        /// 根据输入排序参数进行排序
        /// <para>
        /// orderBy+thenBy
        /// 可指定默认排序方式
        /// </para>
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entities"></param>
        /// <param name="inputParams"></param>
        /// <param name="defultParams"></param>
        /// <returns></returns>
        public static IQueryable<TEntity> AsSortQuery<TEntity, TKey>(this IQueryable<TEntity> entities, BaseSortParams inputParams, Expression<Func<TEntity, TKey>> defultParams)
        {
            var sortParam = inputParams?.GetSort();
            if (sortParam != null)
            {
                entities = AsSortQuery<TEntity>(entities, inputParams);
            }
            else if (defultParams != null)
            {
                entities = entities.OrderByDescending(defultParams);
            }
            return entities;
        }
    }
}
